<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['weiriya:depositRecord:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['weiriya:depositRecord:export']"
        >
          导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="depositRecordList"
      border
      stripe
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="#" type="index" width="55" align="center">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户昵称" align="center" prop="memberName" />
      <el-table-column label="会员类型" align="center" width="120">
        <template slot-scope="scope">
          <el-tag
            :type="
              scope.row.vipType == 1
                ? 'warning'
                : scope.row.vipType == 2
                ? 'danger'
                : scope.row.vipType == 3
                ? 'success'
                : 'info'
            "
            effect="plain"
          >
            {{
              scope.row.vipType == 1
                ? "月卡会员"
                : scope.row.vipType == 2
                ? "季卡会员"
                : scope.row.vipType == 3
                ? "年卡会员"
                : "非会员"
            }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="停车点位" align="center" prop="areaName" />
      <el-table-column label="开始时间" align="center" prop="startTime" />
      <el-table-column label="结束时间" align="center" prop="endTime" />
      <el-table-column label="停车费用" align="center" prop="amount" width="120" />
      <el-table-column label="停车状态" align="center" width="120">
        <template slot-scope="scope">
          <el-tag
            :type="
              scope.row.state == 1
                ? 'success'
                : scope.row.state == 2
                ? 'warning'
                : 'danger'
            "
            effect="plain"
          >
            {{
              scope.row.state == 1
                ? "计费中"
                : scope.row.state == 2
                ? "已结束"
                : "异常结束"
            }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['weiriya:depositRecord:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listDepositRecord, delDepositRecord } from "@/api/weiriya/depositRecord";

export default {
  name: "DepositRecord",
  data() {
    return {
      loading: false, // 遮罩层
      ids: [], // 选中数组
      single: true, // 非单个禁用
      multiple: false, // 非多个禁用
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      depositRecordList: [], // 用户停车记录表格数据
      // 查询参数
      queryParams: {
        memberId: null, // 用户ID
        pageNum: 1,
        pageSize: 10,
        startTime: null,
        endTime: null,
        vipType: null,
      },
      form: {}, // 表单参数
      rules: {}, // 表单校验
    };
  },
  watch: {
    "$route.query": {
      handler(val) {
        setTimeout(() => {
          this.queryParams.memberId = val.id ? val.id : null;
          this.getList();
        }, 300);
      },
      deep: true,
      immediate: true,
    },
  },
  methods: {
    // 查询用户停车记录列表
    getList() {
      this.loading = true;
      listDepositRecord(this.queryParams)
        .then((res) => {
          this.depositRecordList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 删除
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除用户停车记录编号为"' + ids + '"的数据项？')
        .then(() => {
          delDepositRecord(ids).then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          });
        })
        .catch(() => {});
    },
    // 导出
    handleExport() {
      this.download(
        "weiriya/depositRecord/export",
        {
          ...this.queryParams,
        },
        `depositRecord_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
